/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number[][]}
 */
 var pathSum = function(root, targetSum) {
    let res=[];
    if(!root){
        return res;
     }
     let path=[];
    function digui(root,sum){
        if(!root){
            return ;
        }
        else if(!root.left && !root.right){
            if(sum+root.val==targetSum){
                path.push(root.val);
                res.push([...path]);
                path.pop()
            }
            return ;
        }else{
            path.push(root.val);
            digui(root.left,sum+root.val);
            digui(root.right,sum+root.val);
            path.pop();
        }
        
    }
    digui(root,0);
    return res;
};